﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Tailoring
{
    public partial class frmusercontrol : Form
    {
        SqlConnection cn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
        public frmusercontrol()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtpass.Text == txtconfirm.Text && (txtuser.Text != "" & txtpass.Text != "" & txtptip.Text != ""))
            {
                string queryText = "insert into tbllogin(username,pass,passtips)values(@Username,@Password,@PTips)";

                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@Username", txtuser.Text);  // cmd is SqlCommand 
                    cmd.Parameters.AddWithValue("@Password", txtpass.Text);
                    cmd.Parameters.AddWithValue("@PTips", txtptip.Text);
                    int result = (int)cmd.ExecuteNonQuery();
                    if (result > 0)
                    {
                        MessageBox.Show("User Saved Successfully !");
                        txtconfirm.Text = "";
                        txtpass.Text = "";
                        txtptip.Text = "";
                        txtuser.Text = "";
                    }
                    else
                        MessageBox.Show("User Not Saved");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand("clothrate", cn);
                cmd.Parameters.AddWithValue("@id", 1);
                
                cmd.Parameters.AddWithValue("@crate", txtrate.Text);
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.InsertCommand = cmd;
                adapter.InsertCommand.ExecuteNonQuery();
                cmd.Connection.Close();
                MessageBox.Show("Cloth Rate has Been Save Successfully ", "Save Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                this.cn.Close();

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if ( txtpassword.Text != "")
            {
                string queryText = "SELECT Count(*) FROM tbllogin " +
                                   "WHERE username = 'Admin' AND pass = @Password";
                using (SqlConnection cn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true"))
                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    //cmd.Parameters.AddWithValue("@Username", txtusername.Text);  // cmd is SqlCommand 
                    cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
                    int result = (int)cmd.ExecuteScalar();
                    if (result > 0)
                    {
                        MessageBox.Show("Now You Can Configure Users and Other Settings");
                        button1.Enabled = true;
                        button2.Enabled = true;
                        btndeluser.Enabled = true;
                        btnupduser.Enabled = true;
                        button7.Enabled = false;


                    }
                    else
                        MessageBox.Show("Wrong Password Provided try Again");
                }
            }
        }

        private void usermenu_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (txtpass.Text == txtconfirm.Text && (txtuser.Text != "" & txtpass.Text != "" & txtptip.Text != ""))
            {
                string queryText = "insert into tbllogin(username,pass,passtips,question,answer)values(@Username,@Password,@PTips,@question,@ans)";

                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@Username", txtuser.Text);  // cmd is SqlCommand 
                    cmd.Parameters.AddWithValue("@Password", txtpass.Text);
                    cmd.Parameters.AddWithValue("@PTips", txtptip.Text);
                    cmd.Parameters.AddWithValue("@question", cmbq.Text);
                    cmd.Parameters.AddWithValue("@ans", txtans.Text);
                    int result = (int)cmd.ExecuteNonQuery();
                    if (result > 0)
                    {
                        MessageBox.Show("User Saved Successfully !");
                        txtconfirm.Text = "";
                        txtpass.Text = "";
                        txtptip.Text = "";
                        txtuser.Text = "";
                        cmbq.Text = "";
                        txtans.Text = "";
                    }
                    else
                        MessageBox.Show("Either Passwords Don't Match Or Somthing Need To be Fill!");
                }
            }
        }

        private void btndeluser_Click(object sender, EventArgs e)
        {
            if ((txtdeluser.Text != "" & txtdelpass.Text != "" & txtdeluser.Text=="Admin"))
            {
                string queryText = "Delete from tbllogin where (pass=@Password And username=@Username)";

                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    try
                    {
                        cn.Open();
                        cmd.Parameters.AddWithValue("@Username", txtdeluser.Text);  // cmd is SqlCommand 
                        cmd.Parameters.AddWithValue("@Password", txtdelpass.Text);
                        cmd.CommandType = CommandType.Text;
                        int result = (int)cmd.ExecuteNonQuery();
                        if (result > 0)
                        {
                            MessageBox.Show("User Removed Successfully !");
                            btndeluser.Enabled = false;
                        }
                        else
                            MessageBox.Show("Wrong Data Provided or There is No User Name "+txtdeluser.Text.ToUpper()+" "+"Registered");

                    }

                    catch { }
                    finally
                    {
                        cn.Close();
                    }
                }
            }
            else
                MessageBox.Show("Fill Both the Text Fields");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtolduser.Text != "" & txtoldpass.Text != "")
            {
                try
                {
                    string queryText = "SELECT Count(*) FROM tbllogin " +
                                       "WHERE username = @Username AND pass = @Password";
                    using (SqlConnection cn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true"))
                    using (SqlCommand cmd = new SqlCommand(queryText, cn))
                    {
                        cn.Open();
                        cmd.Parameters.AddWithValue("@Username", txtolduser.Text);  // cmd is SqlCommand 
                        cmd.Parameters.AddWithValue("@Password", txtoldpass.Text);
                        int result = (int)cmd.ExecuteScalar();
                        if (result > 0)
                        {

                            txtupdpass.Enabled = true;
                            txtupdconpass.Enabled = true;
                            txtupdtip.Enabled = true;
                            btnupduser.Enabled = true;
                            button3.Enabled = false;
                            txtoldpass.Enabled = false;
                            txtolduser.Enabled = false;

                        }
                        else
                            MessageBox.Show("User Not Found!");
                    }
                }
                catch { }
                    finally
                    {
                        cn.Close();
                    }
            }
        }

        private void btnupduser_Click(object sender, EventArgs e)
        {
            if (txtupdconpass.Text == txtupdpass.Text)
            {
                try
                {
                    string queryText = "update tbllogin set pass=@Password,passtips=@PTips Where username='" + txtolduser.Text + "' And pass='" + txtoldpass.Text + "'";

                    using (SqlCommand cmd = new SqlCommand(queryText, cn))
                    {
                        cn.Open();
                        ;  // cmd is SqlCommand 
                        cmd.Parameters.AddWithValue("@Password", txtupdpass.Text);
                        cmd.Parameters.AddWithValue("@PTips", txtupdtip.Text);
                        int result = (int)cmd.ExecuteNonQuery();
                        if (result > 0)
                        {
                            MessageBox.Show("User updated Successfully !");
                            txtconfirm.Text = "";
                            txtupdpass.Text = "";
                            txtupdtip.Text = "";
                            txtupdconpass.Text = "";
                        }
                        else
                            MessageBox.Show("Either Passwords Don't Match Or Somthing Need To be Fill!");
                    }
                }

                catch { }

                finally
                {
                    cn.Close();
                }
            }
            else
                MessageBox.Show("Passwords Dont Match!");

        }

        private void btnrecover_Click(object sender, EventArgs e)
        {
            if (cmbrq.Text != "" & txtrans.Text != "")
            {
                string queryText = "SELECT pass FROM tbllogin " +
                                   "WHERE question = @ques AND answer = @ans";
                using (SqlConnection cn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true"))
                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@ques", cmbrq.Text);  // cmd is SqlCommand 
                    cmd.Parameters.AddWithValue("@ans", txtrans.Text);
                    SqlDataReader sqldreader = cmd.ExecuteReader();
                    //int result = (int)cmd.ExecuteScalar();
                    if (sqldreader.HasRows)
                    {
                        while (sqldreader.Read())
                            lbpass.Text = sqldreader["pass"].ToString();
                    }
                    else
                        MessageBox.Show("Check Your Question And Answer");
                }

            }
        }
    }
}
